{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content-type:text/html\n",
      "\n",
      "<html>\n",
      "<head>\n",
      "<meta charset=\"utf-8\">\n",
      "<title>Hello World - 我的第一个 CGI 程序！</title>\n",
      "</head>\n",
      "<body>\n",
      "<h2>Hello World! 我是来自菜鸟教程的第一CGI程序</h2>\n",
      "</body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "print \"Content-type:text/html\"\n",
    "print                               # 空行，告诉服务器结束头部\n",
    "print '<html>'\n",
    "print '<head>'\n",
    "print '<meta charset=\"utf-8\">'\n",
    "print '<title>Hello World - 我的第一个 CGI 程序！</title>'\n",
    "print '</head>'\n",
    "print '<body>'\n",
    "print '<h2>Hello World! 我是来自菜鸟教程的第一CGI程序</h2>'\n",
    "print '</body>'\n",
    "print '</html>'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content-type: text/html\n",
      "\n",
      "<meta charset=\"utf-8\">\n",
      "<b>环境变量</b><br>\n",
      "<ul>\n",
      "<li><span style='color:green'>                           TMP </span> : C:\\Users\\JIANGC~1\\AppData\\Local\\Temp </li>\n",
      "<li><span style='color:green'>              PYTHONIOENCODING </span> : 936 </li>\n",
      "<li><span style='color:green'>                  COMPUTERNAME </span> : DESKTOP-F2P1GL1 </li>\n",
      "<li><span style='color:green'>              CONDA_PYTHON_EXE </span> : F:\\Program\\Anaconda2\\python.exe </li>\n",
      "<li><span style='color:green'>                    USERDOMAIN </span> : DESKTOP-F2P1GL1 </li>\n",
      "<li><span style='color:green'>        PROCESSOR_ARCHITECTURE </span> : AMD64 </li>\n",
      "<li><span style='color:green'>                  PSMODULEPATH </span> : C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules </li>\n",
      "<li><span style='color:green'>            COMMONPROGRAMFILES </span> : C:\\Program Files\\Common Files </li>\n",
      "<li><span style='color:green'>          PROCESSOR_IDENTIFIER </span> : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel </li>\n",
      "<li><span style='color:green'>                  PROGRAMFILES </span> : C:\\Program Files </li>\n",
      "<li><span style='color:green'>            PROCESSOR_REVISION </span> : 4e03 </li>\n",
      "<li><span style='color:green'>                    MAVEN_HOME </span> : F:\\Program\\apache-maven-3.5.3 </li>\n",
      "<li><span style='color:green'>                    SYSTEMROOT </span> : C:\\WINDOWS </li>\n",
      "<li><span style='color:green'>                      CLICOLOR </span> : 1 </li>\n",
      "<li><span style='color:green'>             PROGRAMFILES(X86) </span> : C:\\Program Files (x86) </li>\n",
      "<li><span style='color:green'>                    DRIVERDATA </span> : C:\\Windows\\System32\\Drivers\\DriverData </li>\n",
      "<li><span style='color:green'>                   CONDA_SHLVL </span> : 1 </li>\n",
      "<li><span style='color:green'>                          TEMP </span> : C:\\Users\\JIANGC~1\\AppData\\Local\\Temp </li>\n",
      "<li><span style='color:green'>       COMMONPROGRAMFILES(X86) </span> : C:\\Program Files (x86)\\Common Files </li>\n",
      "<li><span style='color:green'>                  CONDA_PREFIX </span> : F:\\Program\\Anaconda2 </li>\n",
      "<li><span style='color:green'>             CHOCOLATEYINSTALL </span> : C:\\ProgramData\\chocolatey </li>\n",
      "<li><span style='color:green'>      CHOCOLATEYLASTPATHUPDATE </span> : Fri Jan 25 16:39:02 2019 </li>\n",
      "<li><span style='color:green'>               ALLUSERSPROFILE </span> : C:\\ProgramData </li>\n",
      "<li><span style='color:green'>                ZOOKEEPER_HOME </span> : F:\\Program\\zookeeper-3.4.10\\zookeeper-3.4.10 </li>\n",
      "<li><span style='color:green'>                      HOMEPATH </span> : \\Users\\jiangcheng </li>\n",
      "<li><span style='color:green'>     USERDOMAIN_ROAMINGPROFILE </span> : DESKTOP-F2P1GL1 </li>\n",
      "<li><span style='color:green'>                     JAVA_HOME </span> : C:\\Program Files\\Java\\jdk1.8.0_172 </li>\n",
      "<li><span style='color:green'>           JPY_INTERRUPT_EVENT </span> : 1984 </li>\n",
      "<li><span style='color:green'>                   ERLANG_HOME </span> : C:\\Program Files\\erl9.3 </li>\n",
      "<li><span style='color:green'>                      USERNAME </span> : jiangcheng </li>\n",
      "<li><span style='color:green'>                   LOGONSERVER </span> : \\\\DESKTOP-F2P1GL1 </li>\n",
      "<li><span style='color:green'>                        PROMPT </span> : (base) $P$G </li>\n",
      "<li><span style='color:green'>                       COMSPEC </span> : C:\\WINDOWS\\system32\\cmd.exe </li>\n",
      "<li><span style='color:green'>                JPY_PARENT_PID </span> : 2188 </li>\n",
      "<li><span style='color:green'>                   PROGRAMDATA </span> : C:\\ProgramData </li>\n",
      "<li><span style='color:green'>       CHOCOLATEYTOOLSLOCATION </span> : F:\\Program </li>\n",
      "<li><span style='color:green'>                  LOCALAPPDATA </span> : C:\\Users\\jiangcheng\\AppData\\Local </li>\n",
      "<li><span style='color:green'>                          TERM </span> : xterm-color </li>\n",
      "<li><span style='color:green'>         CONDA_PROMPT_MODIFIER </span> : (base)  </li>\n",
      "<li><span style='color:green'>                          PATH </span> : F:\\Program\\Anaconda2;F:\\Program\\Anaconda2\\Library\\mingw-w64\\bin;F:\\Program\\Anaconda2\\Library\\usr\\bin;F:\\Program\\Anaconda2\\Library\\bin;F:\\Program\\Anaconda2\\Scripts;F:\\Program\\Anaconda2\\bin;F:\\Program\\Anaconda2;F:\\Program\\Anaconda2\\Library\\mingw-w64\\bin;F:\\Program\\Anaconda2\\Library\\usr\\bin;F:\\Program\\Anaconda2\\Library\\bin;F:\\Program\\Anaconda2\\Scripts;F:\\Program\\Anaconda2\\Library\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Java\\jdk1.8.0_172\\bin;C:\\Program Files\\erl9.3\\bin;F:\\Program\\apache-maven-3.5.3\\bin;F:\\Program\\zookeeper-3.4.10\\zookeeper-3.4.10\\bin;C:\\WINDOWS\\System32\\OpenSSH\\;F:\\Program\\gradle-4.9\\bin;C:\\Program Files\\Docker\\Docker\\Resources\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Program Files\\Microsoft VS Code\\bin;C:\\ProgramData\\chocolatey\\bin;F:\\Program Files\\nodejs\\;F:\\Program Files\\Git\\cmd;C:\\Users\\jiangcheng\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Users\\jiangcheng\\AppData\\Roaming\\npm; </li>\n",
      "<li><span style='color:green'>                    MPLBACKEND </span> : module://ipykernel.pylab.backend_inline </li>\n",
      "<li><span style='color:green'>                     GIT_PAGER </span> : cat </li>\n",
      "<li><span style='color:green'>                   SESSIONNAME </span> : Console </li>\n",
      "<li><span style='color:green'>                       PATHEXT </span> : .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC </li>\n",
      "<li><span style='color:green'>                         PAGER </span> : cat </li>\n",
      "<li><span style='color:green'>                        WINDIR </span> : C:\\WINDOWS </li>\n",
      "<li><span style='color:green'>                   GRADLE_HOME </span> : F:\\Program\\gradle-4.9 </li>\n",
      "<li><span style='color:green'>               MOZ_PLUGIN_PATH </span> : F:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\plugins\\ </li>\n",
      "<li><span style='color:green'>                     HOMEDRIVE </span> : C: </li>\n",
      "<li><span style='color:green'>             CONDA_DEFAULT_ENV </span> : base </li>\n",
      "<li><span style='color:green'>                   SYSTEMDRIVE </span> : C: </li>\n",
      "<li><span style='color:green'>                        PUBLIC </span> : C:\\Users\\Public </li>\n",
      "<li><span style='color:green'>          NUMBER_OF_PROCESSORS </span> : 4 </li>\n",
      "<li><span style='color:green'>                       APPDATA </span> : C:\\Users\\jiangcheng\\AppData\\Roaming </li>\n",
      "<li><span style='color:green'>               PROCESSOR_LEVEL </span> : 6 </li>\n",
      "<li><span style='color:green'>                  PROGRAMW6432 </span> : C:\\Program Files </li>\n",
      "<li><span style='color:green'>            COMMONPROGRAMW6432 </span> : C:\\Program Files\\Common Files </li>\n",
      "<li><span style='color:green'>                            OS </span> : Windows_NT </li>\n",
      "<li><span style='color:green'>                     CONDA_EXE </span> : F:\\Program\\Anaconda2\\Scripts\\conda.exe </li>\n",
      "<li><span style='color:green'>           IPY_INTERRUPT_EVENT </span> : 1984 </li>\n",
      "<li><span style='color:green'>                   USERPROFILE </span> : C:\\Users\\jiangcheng </li>\n",
      "</ul>\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "print \"Content-type: text/html\"\n",
    "print\n",
    "print \"<meta charset=\\\"utf-8\\\">\"\n",
    "print \"<b>环境变量</b><br>\";\n",
    "print \"<ul>\"\n",
    "for key in os.environ.keys():\n",
    "    print \"<li><span style='color:green'>%30s </span> : %s </li>\" % (key,os.environ[key])\n",
    "print \"</ul>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content-type:text/html\n",
      "\n",
      "<html>\n",
      "<head>\n",
      "<meta charset=\"utf-8\">\n",
      "<title>菜鸟教程 CGI 测试实例</title>\n",
      "</head>\n",
      "<body>\n",
      "<h2>None官网：None</h2>\n",
      "</body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "# CGI处理模块\n",
    "import cgi, cgitb \n",
    "\n",
    "# 创建 FieldStorage 的实例化\n",
    "form = cgi.FieldStorage() \n",
    "\n",
    "# 获取数据\n",
    "site_name = form.getvalue('name')\n",
    "site_url  = form.getvalue('url')\n",
    "\n",
    "print \"Content-type:text/html\"\n",
    "print\n",
    "print \"<html>\"\n",
    "print \"<head>\"\n",
    "print \"<meta charset=\\\"utf-8\\\">\"\n",
    "print \"<title>菜鸟教程 CGI 测试实例</title>\"\n",
    "print \"</head>\"\n",
    "print \"<body>\"\n",
    "print \"<h2>%s官网：%s</h2>\" % (site_name, site_url)\n",
    "print \"</body>\"\n",
    "print \"</html>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content-type:text/html\n",
      "\n",
      "<html>\n",
      "<head>\n",
      "<meta charset=\"utf-8\">\n",
      "<title>菜鸟教程 CGI 测试实例</title>\n",
      "</head>\n",
      "<body>\n",
      "<h2>None官网：None</h2>\n",
      "</body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "# CGI处理模块\n",
    "import cgi, cgitb \n",
    "\n",
    "# 创建 FieldStorage 的实例化\n",
    "form = cgi.FieldStorage() \n",
    "\n",
    "# 获取数据\n",
    "site_name = form.getvalue('name')\n",
    "site_url  = form.getvalue('url')\n",
    "\n",
    "print \"Content-type:text/html\"\n",
    "print\n",
    "print \"<html>\"\n",
    "print \"<head>\"\n",
    "print \"<meta charset=\\\"utf-8\\\">\"\n",
    "print \"<title>菜鸟教程 CGI 测试实例</title>\"\n",
    "print \"</head>\"\n",
    "print \"<body>\"\n",
    "print \"<h2>%s官网：%s</h2>\" % (site_name, site_url)\n",
    "print \"</body>\"\n",
    "print \"</html>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
